package com.gdssoft.pdaweb.service.impl;

import java.util.Calendar;
import java.util.List;

import com.gdssoft.core.command.QueryFilter;
import com.gdssoft.core.service.impl.BaseServiceImpl;
import com.gdssoft.core.util.DateUtil;
import com.gdssoft.pdaweb.dao.AutoPlanDao;
import com.gdssoft.pdaweb.model.AutoPlan;
import com.gdssoft.pdaweb.service.AutoPlanService;
import com.gdssoft.pdaweb.service.am.AmPlanService;
import com.gdssoft.pdaweb.service.water.WaterMeterPlanService;


public class AutoPlanServiceImpl extends BaseServiceImpl<AutoPlan> implements AutoPlanService{
    private AutoPlanDao dao;
    private AmPlanService amPlanService;
    private WaterMeterPlanService waterMeterPlanService;
	
	public AutoPlanServiceImpl(AutoPlanDao dao) {
		super(dao);
		this.dao=dao;
	}

	public void setAmPlanService(AmPlanService amPlanService) {
		this.amPlanService = amPlanService;
	}

	public void setWaterMeterPlanService(WaterMeterPlanService waterMeterPlanService) {
		this.waterMeterPlanService = waterMeterPlanService;
	}

	@Override
	public AutoPlan getAmOpenAutoPlan(Long planId) {
		return getOpenAutoPlan(AutoPlan.TYPE_AM, planId);
	}

	@Override
	public AutoPlan getWaterOpenAutoPlan(Long planId) {
		return getOpenAutoPlan(AutoPlan.TYPE_WATER, planId);
	}
	/**
	 * 根据编号和类别获取计划
	 * @param type 自动执行计划的类别
	 * @param planId 计划的编号
	 * @return 计划的单一实例
	 */
	private AutoPlan getOpenAutoPlan(Integer type, Long planId) {
		QueryFilter filter = new QueryFilter(0, 200);
		filter.addFilter("Q_type_N_EQ", type.toString());
		filter.addFilter("Q_status_N_EQ", AutoPlan.STATUS_OPEN.toString());
		filter.addFilter("Q_pId_L_EQ", planId.toString());
		List<AutoPlan> list = dao.getAll(filter);
		if (list.size() > 0) {
			return list.get(0);
		} else {
			return null;
		}
	}

	@Override
	public List<AutoPlan> listAmAutoPlanToday() {
		return listAutoPlanToday(AutoPlan.TYPE_AM);
	}

	@Override
	public List<AutoPlan> listWaterAutoPlanToday() {
		return listAutoPlanToday(AutoPlan.TYPE_WATER);
	}
	/**
	 * 根据类别获取今天待执行的计划列表
	 * @param type 自动执行计划的类别
	 * @return 计划的列表
	 */
	private List<AutoPlan> listAutoPlanToday(Integer type) {
		String fromDate = DateUtil.dateFormat(Calendar.getInstance().getTime(), "yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DAY_OF_MONTH, 1);
		String toDate = DateUtil.dateFormat(cal.getTime(), "yyyy-MM-dd");
		QueryFilter filter = new QueryFilter(0, 200);
		filter.addFilter("Q_type_N_EQ", type.toString());
		filter.addFilter("Q_status_N_EQ", AutoPlan.STATUS_OPEN.toString());
		filter.addFilter("Q_executeDate_D_GE", fromDate);
		filter.addFilter("Q_executeDate_D_LT", toDate);
		return dao.getAll(filter);
	}

	@Override
	public boolean executePlan(AutoPlan autoPlan) throws Exception {
		boolean isSuccess = false;
		
		if (autoPlan.getType() == AutoPlan.TYPE_AM) {
			isSuccess = amPlanService.execute(autoPlan.getpId(), 
					autoPlan.getBeginTime(), 
					autoPlan.getEndTime(), 
					autoPlan.getExecutor());
		} else {
			isSuccess = waterMeterPlanService.execute(autoPlan.getpId(), 
					autoPlan.getBeginTime(), 
					autoPlan.getEndTime(), 
					autoPlan.getExecutor());
		}
		
		if (isSuccess) {
			//安排下一次的自动执行计划
			AutoPlan nextAutoPlan = autoPlan.nextMonthAutoPlan("AutoPlanService");
			if (null != nextAutoPlan) {
				try {
					this.save(nextAutoPlan);
				} catch (Exception e) {
					Exception e1 = new Exception("安排下一次的自动执行计划失败", e);
					throw e1;
				}
			}
			
			//关闭自动执行计划
			autoPlan.setAutoPlanClosed();
			try {
				this.save(autoPlan);
			} catch (Exception e) {
				Exception e2 = new Exception("关闭自动执行计划失败，ID：" + autoPlan.getId(), e);
				throw e2;
			}
		}
		
		return isSuccess;
	}

}
